Keeps only the items of target that the script test says yes to, and takes out the ones it says no to - in the list itself, with no second list made. The items that stay keep their order. test is a script that takes one argument (the item) and gives back true or false: the same kind of script list_filter uses, where true means keep. Do not add to or take from target inside that script.
list_keep(target, test)| Name | Type |
|---|---|
target | list |
test | script |
Nothing.
Takes out of target, a list, every item that a script says no to, and keeps the ones it says yes to. The script is asked about each item in turn. It answers true to keep the item and false to take it out. The items that stay keep the order they had. It changes target itself and makes no second list.
list_keep(scores, is_high);If scores holds 30, 80, 55 and 10 and is_high says yes to 50 or more, scores holds 80 and 55 afterwards, in that order, and list_length says 2.
list_filter asks the same question with the same kind of script, but it leaves target alone and makes a new list of the items that passed. list_keep does the job inside the list you already have.
list_filter | list_keep | |
|---|---|---|
what it does to target | nothing: it is not changed | the items the script said no to are taken out |
| memory | a whole second list, as big as target can ever be | none |
| where you can write it | only as the whole right-hand side of the line that makes a variable | on a line of its own, anywhere, as often as you like |
| works on | a list, an array or a grid | a list only |
Use list_filter when you need the original as well. Use list_keep when you do not, and above all in a game that filters over and over: bullets that are still flying, enemies that are not beaten yet. It costs no memory, so doing it every frame is fine.
The second thing you give is a script that answers true or false for one item, the same kind of script list_filter takes, and it has to follow the same rules: it takes exactly one parameter, which is where each item arrives, and the items have to fit that parameter's type. The script is_high above can be handed to either function.
// scripts/is_high.gbml
function is_high(score: int): bool {
return score >= 50;
}While list_keep is working through the list, the script must not add items to that list or take items out of it. GBML cannot see it happening. If it does happen, nothing breaks: nothing outside the list is ever read or written, and the pass always ends. But which items are left afterwards is not promised, so do not rely on it. Looking at the list from inside the script does no harm, but do not count on what you find there: the items that stay are moved down as the pass goes, so the list is part way between the old one and the new one.
An array and a grid have a fixed number of places, so nothing can be taken out of them, and list_keep on one is refused when the project is checked. A stack, a queue, a set and a map have no "each item in order" to ask about, so they have no list_keep either. To get the items an array or a grid passes, use array_filter or grid_filter, which make a list.
| You do this | What happens |
|---|---|
| keep from a list with no items | nothing happens, and the script is never called |
| the script says no to every item | the list is empty afterwards |
| the script says yes to every item | the list is left as it is |
| a list of text, structs or references | works. Nothing is compared, the script decides |
give a script that is not a script, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or false | refused when checked, and the message says which |
| give it an array, a grid, a stack or a queue | refused when checked |
| use it where a value is needed | refused when checked. It gives nothing back |
One call of your script for each item the list holds, and at most one move of an item for each item. Nothing is set aside, whatever the size of the list. Taking items out with list_remove in a loop costs more, because every removal moves all the items after it. No timing was measured.
/// @type {list<int>}
var scores = [30, 80, 55, 10];
list_keep(scores, is_high); // 80, 55
var how_many = list_length(scores); // 2
var best = scores[0]; // 80One pass over the list_length(target) items the list holds, from the first to the last, with a second place behind it: each item is copied once, the script is asked about the copy, and an item it says yes to is moved down to the next free place at the front (it does not move at all while nothing before it has been taken out). When the pass is over, the places after the last kept item are taken off the end of the list. O(n): one call of your script per item and at most one move per item. Unlike list_filter it sets no memory aside - no second list, whatever the capacity - so it is the one to use in a game that filters every frame. The kept items keep their order. The script has to give back true or false and take exactly one argument whose type accepts the items. Do not add to or take from the list inside the script: GBML cannot see that it happens. If it does, nothing outside the list is read or written (the length is read again at every step, the item is copied before the script is asked about it, and a place is checked against the length before it is written), but which items are left is not promised. It works on a list only: an array and a grid have a fixed number of places, so nothing can be taken out of them, and a stack, queue, set or map has no "each item in order". No timing was measured.